<import file="block/common.html" name="url" :tpl_cfg="['urlName'=>$title,'backList'=>'返回列表']"/>
<div class="layui-fluid">
    <div class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入标题"
                    value="<{$notice['title']??''}>" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">图片</label>
            <div class="layui-input-inline" style="width:420px;" id="photopre">
                <?php if(!empty($notice['img'])){ ?>
                    <img src="<{$notice['img']??''}>" width="150" height="120" >
                <?php  } ?>   
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required"></label>
            <div class="layui-input-block">
                <input type="text" id="imageval" name="img" value="<{$notice['img']??''}>" class="layui-input">
                <button type="button" class="layui-btn" id="thumimg">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                </button>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="content" id="content" class="layui-textarea" placeholder="请输入内容"><{$notice['content']??''}></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="审核"
                    <?php if(!empty($notice) && $notice["status"] ==1){ echo 'checked=""';} ?>>
                <input type="radio" name="status" value="0" title="存草稿"
                    <?php if(!empty($notice) && $notice["status"] ==0){ echo 'checked=""';} ?>>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="hidden" name="id" value="<{$notice['id']??''}>">
                <button class="layui-btn" lay-submit lay-filter="saveBtn"><i class="layui-icon">&#x1005;</i><?php echo lang('save');?></button>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="<?php echo U_RES;?>/lib/tinymce/tinymce.min.js?v=5.10.7"></script>
<script>
    layui.define(function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            $ = layui.$;
        form.render();
        tinymce.init({
			selector: '#content',
			language: 'zh_CN',
			relative_urls: false,
			remove_script_host: true,
			plugins: 'preview searchreplace autolink directionality visualblocks visualchars image link media template code codesample table charmap pagebreak nonbreaking anchor insertdatetime advlist lists wordcount help emoticons autoresize axupimgs',
			toolbar: 'code | undo redo restoredraft | cut copy paste | forecolor bold italic underline link | alignleft aligncenter alignright alignjustify outdent indent | fontselect fontsizeselect | table <?php if(SysRolCk("upload_upload")){ ?>image<?php } ?> charmap emoticons insertdatetime preview | <?php if(SysRolCk("upload_upload")){ ?>axupimgs<?php } ?>',
			//引用lineheight插件，会导致  Uncaught SyntaxError: Unexpected token '<'  错误，暂时未解决！！
			autosave_interval: "20s",//自动存稿时间
			font_formats: '微软雅黑=Microsoft YaHei,sans-serif;仿宋_GB2312=仿宋_GB2312,Microsoft YaHei,sans-serif;宋体=宋体;仿宋=仿宋;黑体=黑体',
			height: 650, //编辑器高度
			min_height: 400,
			lineheight_formats : '1 1.5 2 2.5 3',
			draggable_modal: true,
			images_upload_base_path: '/',
			images_upload_handler: function (blobInfo, succFun, failFun) {
			var xhr, formData;
			var file = blobInfo.blob();//转化为易于理解的file对象
			xhr = new XMLHttpRequest();
			xhr.withCredentials = false;
			xhr.open('POST', '<{CAUrl("upload","upload")}>&up=img&tinymce');
			xhr.onload = function() {
				var json;
				if (xhr.status != 200) {
					failFun('HTTP Error: ' + xhr.status);
					return;
				}
				json = JSON.parse(xhr.responseText);
				if (!json || typeof json.location != 'string') {
					failFun('Invalid JSON: ' + xhr.responseText);
					return;
				}
				succFun(json.location);
			};
			formData = new FormData();
			formData.append('file', file, file.name );
			xhr.send(formData);
		}
		});
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.content = tinyMCE.activeEditor.getContent();
            $.post('<{CAUrl("notice",ROUTE["act"])}>', data.field, function (res) {
                if (res.status == 1) {
                    layer.msg(res.info, { icon: 1, time: 1000 }, function () {
                        loadMain('<{CAUrl("notice","index")}>');
                    });
                } else {
                    layer.msg(res.info, { icon: 2, anim: 6, time: 5000 });
                }
            });
            return false;
        });
        var uploadInst = upload.render({
            elem: '#thumimg' //绑定元素
            , url: '<{CAUrl("upload","upload")}>&up=img' //图片上传接口
            , size: 1000
            , accept: 'images'
            , acceptMime: 'image/*'
            ,acceptMime: 'image/jpg, image/png,image/jpeg'
            , done: function (res) {
                if (res.status == 1) {
                    $.post("<{CAUrl('upload','Upload')}>&up=img&del", { url: "<{$notice['img']??''}>"});//删除旧文件
                    $('#imageval').val(res.result[0].src);
                    $('#photopre').html('<img src="'+res.result[0].src+'" width="150" height="120" >');
                    layer.msg(res.info);
                } else {
                    layer.msg(res.info);
                }
            }
            , error: function () {
                //请求异常回调
            }
        });

    });
</script>